//
//  UIApplication+MKSAdd.swift
//  MeetCarefree
//
//  Created by MK on 2021/6/10.
//  Copyright © 2021 xxf. All rights reserved.
//

import Foundation
import UIKit

extension UIApplication {
    @objc class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        
        if let tabbarCtrl = base as? UITabBarController {
            if let selectedVC = tabbarCtrl.selectedViewController {
                return topViewController(base: selectedVC)
            }
        }
        
        if let nav = base as? UINavigationController {
            return topViewController(base: nav.visibleViewController)
        }
        
        if let presented = base?.presentedViewController {
            return topViewController(base: presented);
        }
        return base;
    }
    
    @objc class func exitApplication(){
        if let win = UIApplication.shared.delegate?.window {
            if let window = win {
                UIView.animate(withDuration: 1.0) {
                    window.alpha = 0
                    window.frame = CGRect(x: window.bounds.size.height/2,
                                          y: window.bounds.size.width/2,
                                          width: 0, height: 0
                    )
                } completion: { finish in
                    exit(0)
                }
            }
        }
    }
}
